package com.qk.controller;

import com.qk.anno.Log;
import com.qk.common.Result;
import com.qk.common.PageResult;
import com.qk.entity.Role;
import com.qk.server.RoleServer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/roles")
@RestController
public class RoleController {
    @Autowired
    private RoleServer roleServer;
    //查询列表
    @GetMapping
    public Result findRoleByList(String name, String label,
                                 @RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "10") Integer pageSize)
    {
        PageResult<Role> pageResult =roleServer.findRoleByList(name,label,page,pageSize);
        log.info("查询角色列表成功{}",pageResult);
        return Result.success(pageResult);
    }
    //删除数据
    @Log
    @DeleteMapping("/{id}")
    public Result delRoleById(@PathVariable Integer id){
        roleServer.deleteById(id);
        log.info("删除角色成功{}",id);
        return Result.success();
    }
    //添加数据
    @Log
    @PostMapping
    public Result addRole(@RequestBody Role role){
        roleServer.addRole(role);
        log.info("添加角色成功{}",role);
        return Result.success();
    }
    //根据Id查询数据
    @GetMapping("/{id}")
    public Result findRoleById(@PathVariable Integer id){
        Role role = roleServer.findRoleById(id);
        log.info("id:{}为:{}",id,role);
        return Result.success(role);
    }
    //修改数据
    @Log
    @PutMapping
    public Result updateRole(@RequestBody Role role){
        roleServer.updateRole(role);
        log.info("修改成功:{}",role);
        return Result.success();
    }
    //查询所有数据
    @GetMapping("/list")
    public Result findAll(){
        List<Role> roles = roleServer.findRoleAll();
        log.info("查询所有数据成功:{}",roles);
        return Result.success(roles);
    }
}
